package com.my.base64;

import cn.hutool.core.util.StrUtil;

import java.nio.charset.StandardCharsets;
import java.util.Base64;

/**
 * Java Base64编码相关操作
 *
 * 参考：
 *      1. Base64 wiki: https://zh.wikipedia.org/wiki/Base64
 *      2. Base64 Decode and Encode - Online : https://www.base64decode.org/
 *
 * @author: marshal
 * @date: 2021-11-09 7:28 PM
 */
public class Base64Test {
    public static void main(String[] args) {
        String english = "to the time to life";
        String chinese = "给时光以生命";

        System.out.println(encode(english));
        System.out.println(encode(chinese));
        System.out.println(decode(encode(chinese)));
    }

    private static String encode(String text) {
        return Base64.getEncoder().encodeToString(text.getBytes(StandardCharsets.UTF_8));
    }

    private static String decode(String text) {
        return StrUtil.str(Base64.getDecoder().decode(text), StandardCharsets.UTF_8);
    }
}
